import threading

class RWlock(object):

    def __init__(self):
        self._lock = threading.Lock()
        self._extra = threading.Lock()
        self.read_number = 0

    def read_lock(self):
        with self._extra:
            self.read_number = self.read_number + 1
            if self.read_number == 1:
                self._lock.acquire()

    def read_unlock(self):
        with self._extra:
            self.read_number = self.read_number - 1
            if self.read_number == 0:
                self._lock.release()

    def write_lock(self):
        self._lock.acquire()

    def write_unlock(self):
        self._lock.release()
